package org.example.chm;/*
 * title:
 * author:
 * date:
 * description:
 * */

import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.microsoft.chm.ChmParser;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.tika.sax.ToXMLContentHandler;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ChmParserToXML {
    public static void main(String[] args) {
        String chmFilePath = "C:\\Users\\11924\\Java\\supportSearch\\BTS3900&BTS5900 V100R019C10SPC210 Reference-CN\\BTS3900&BTS5900 V100R019C10SPC210 MML命令参考.chm";
        String outputXmlPath = "C:\\Users\\11924\\Java\\output\\output\\output_MML命令参考.xml";

        try (FileInputStream inputStream = new FileInputStream(new File(chmFilePath))) {
            Tika tika = new Tika();
            ChmParser chmParser = new ChmParser();
            BodyContentHandler handler = new BodyContentHandler(-1); // No limit
            Metadata metadata = new Metadata();
            ParseContext parseContext = new ParseContext();

            chmParser.parse(inputStream, handler, metadata, parseContext);

            // 使用 jsoup 解析 HTML
            String htmlContent = handler.toString();
            Document jsoupDoc = Jsoup.parse(htmlContent);

            // 准备创建输出 XML 文件
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // 创建根元素
            org.w3c.dom.Document outputDoc = docBuilder.newDocument();
            org.w3c.dom.Element rootElement = outputDoc.createElement("chmContents");
            outputDoc.appendChild(rootElement);

            // 获取所有标题
            Elements titles = jsoupDoc.select("h1, h2, h3");

            for (Element title : titles) {
                // 创建标题元素
                org.w3c.dom.Element titleElement = outputDoc.createElement("title");
                titleElement.appendChild(outputDoc.createTextNode(title.text()));
                rootElement.appendChild(titleElement);

                // 输出该标题下直到下一个标题之前的所有内容
                Element sibling = title.nextElementSibling();
                while (sibling != null && !(sibling.tagName().equals("h1") || sibling.tagName().equals("h2") || sibling.tagName().equals("h3"))) {
                    org.w3c.dom.Element contentElement = outputDoc.createElement("content");
                    contentElement.appendChild(outputDoc.createTextNode(sibling.text()));
                    titleElement.appendChild(contentElement);
                    sibling = sibling.nextElementSibling();
                }
            }

            // 写入 XML 文件
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource source = new DOMSource(outputDoc);
            StreamResult result = new StreamResult(new FileOutputStream(outputXmlPath));

            transformer.transform(source, result);

            System.out.println("File saved to " + outputXmlPath);

        } catch (IOException | TikaException | SAXException | ParserConfigurationException | TransformerException e) {
            e.printStackTrace();
        }
    }

}
